﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System;

namespace Client.star
{
    /// <summary>
    /// Interaction logic for StarName.xaml
    /// </summary>
    public partial class StarName : UserControl
    {
        public StarName()
        {
            InitializeComponent();

            TextBoxEnabledChanged += new EventHandler(StarName_TextBoxEnabledChanged);

            border = new Border();
            border.CornerRadius = new CornerRadius(5, 5, 5, 5);
            border.BorderBrush = Brushes.Blue;
            border.BorderThickness = new Thickness(1);
            border.VerticalAlignment = VerticalAlignment.Center;
            border.HorizontalAlignment = HorizontalAlignment.Center;
            border.Padding = new Thickness(1);

            textBox = new TextBox();
            textBox.MaxLength = 12;

            textBox.Padding = new Thickness(3, 0, 4, 0);
            textBox.BorderThickness = new Thickness(0);
            textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
            textBox.Loaded += new RoutedEventHandler(textBox_Loaded);
            textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus);
            textBox.MouseDoubleClick += new MouseButtonEventHandler(textBox_MouseDoubleClick);
            textBox.MouseDown += new MouseButtonEventHandler(textBox_MouseDown);
            textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
            textBox.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(textBox_LostKeyboardFocus);

            border.Child = textBox;
        }

        

        #region Events
        void StarName_TextBoxEnabledChanged(object sender, EventArgs e)
        {
            if (TextBoxEnabled)
                StarMap.StarProdToggleAllowed = false;
            else
                StarMap.StarProdToggleAllowed = true;
        }

        #region label1 events
        private void label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            textBox.Text = label1.Content.ToString();
            Content = border;
            TextBoxEnabled = true;
            e.Handled = true;
        }
        private void label1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        } 
        #endregion

        #region textBox events
        void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            label1.Content = textBox.Text;
            Content = border1;
            TextBoxEnabled = false;
        }
        void textBox_Loaded(object sender, RoutedEventArgs e)
        {
            textBox.Focus();
            textBox.Select(0, textBox.Text.Length);
        }
        void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            border.Measure(new Size(100D, 30D));
            double x = BelongingStar.Location.X + BelongingStar.image1.ActualWidth / 2 - this.border.DesiredSize.Width / 2;
            double y = BelongingStar.Location.Y + BelongingStar.image1.ActualHeight;
            BelongingStar.LocationName = new Point(x, y);
            Canvas.SetLeft(this, BelongingStar.LocationName.X);
            Canvas.SetTop(this, BelongingStar.LocationName.Y);
        }
        void textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            label1.Content = textBox.Text;
            Content = border1;
            TextBoxEnabled = false;
        }
        void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                label1.Content = textBox.Text;
                Content = border1;
                TextBoxEnabled = false;
            }
        }
        void textBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            label1.Content = textBox.Text;
            Content = border1;
            TextBoxEnabled = false;
        }
        void textBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }  
        #endregion
        #endregion

        #region Properties
        public bool TextBoxEnabled
        {
            get { return textBoxEnabled; }
            set
            {
                textBoxEnabled = value;
                if (TextBoxEnabledChanged != null)
                    TextBoxEnabledChanged(this, new EventArgs());
            }
        }
        public Star BelongingStar
        {
            get { return belongingStar; }
            set { belongingStar = value; }
        }
        #endregion

        #region Fields
        TextBox textBox;
        Border border;
        Star belongingStar;
        bool textBoxEnabled = false;
        public event EventHandler TextBoxEnabledChanged;
        #endregion    
    }
}
